package auth

import (
	shadow "FlyFight/Internal/Shadow"
	"FlyFight/sql"
	. "FlyFight/typego"
	"errors"
	"log"
)

// 登录
func Login(usr string ,pass string) (string,error){
	var p Player
	if err:=sql.GetPlayerByName(usr,&p);err!=nil{
		log.Println("用户不存在")
		return "",err
	}

	if !sql.CheckPass(p.Uid,pass){
		return "",errors.New("密码错误")
	}
	

	token,err:=shadow.GenerateJWT(p.Uid,usr)
	if err!=nil{
		log.Println("生成token失败")
		return "",err
	}
	return token,nil
}

func CheckLogin(token string) error {
	if _,err:=shadow.ParseToken(token);err!=nil{
		log.Println("token解析失败")
		return err
	}
	return nil
}